import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

vote_rating = pd.read_csv("DSProject/csv/vote_rating.csv")
mean_value = vote_rating["vote"].mean()
sns.set_theme()
fig1, axes1 = plt.subplots(1, 2, figsize=(15, 4.5))
sns.barplot(
    data=vote_rating, x="rating_group", y="num_votes", hue="rating_group", ax=axes1[0]
)
sns.barplot(
    data=vote_rating.sort_values("num_votes"),
    x="votes_group",
    y="average_rating",
    hue="votes_group",
    ax=axes1[1],
)
axes1[1].tick_params(axis="x", rotation=90)
fig1.suptitle("The relationship between votes and rating")
# 保存图形为 PNG 格式
plt.savefig(r"DSProject\View\Figure\vote_rating.png", dpi=300, bbox_inches="tight")
# 关闭图形窗口
plt.close()

# 选取评分前10%的和投票数后10%（小众）电影数据，分析高分电影投票情况
num_movies = vote_rating["tconst"].count()
select_num = int(num_movies / 10)
fig2, axes2 = plt.subplots(1, 2, figsize=(15, 4.5))
# 绘制前10%分数的电影数据
sns.barplot(
    data=vote_rating.iloc[-select_num:, :],
    x="rating_group_detailed",
    y="num_votes",
    hue="rating_group_detailed",
    ax=axes2[0],
)
# 绘制后20%投票的电影数据
sns.violinplot(
    data=vote_rating.sort_values("num_votes").iloc[: select_num * 2, :],
    x="votes_group_detailed",
    y="average_rating",
    hue="votes_group_detailed",
    ax=axes2[1],
)
axes2[0].set_title("The relationship between high rating(Top 10%) and votes")
axes2[1].set_title(
    "The relationship between low votes(special-interst)(Last 20%) and rating"
)
axes2[1].tick_params(axis="x", rotation=90)

# 保存图形为 PNG 格式
plt.savefig(
    r"DSProject\View\Figure\low_votes_high_rating.png", dpi=300, bbox_inches="tight"
)
# 关闭图形窗口
plt.close()
